package mapper

import (
	"github.com/beego/beego/v2/client/orm"
)

type NoteCodeMapper struct{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (mapper *NoteCodeMapper) CountNoteCode(code string, noteid uint64, systemId uint64) int {
	db := orm.NewOrm()
	var total int
	err := db.Raw("select count(1) from xk_note_code where opid = ? and code = ? and mark = 0 and system_id = ?", noteid, code, systemId).QueryRow(&total)
	if nil != err {
		return 0
	}
	return total
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据code和小册id删除兑换码
 */
func (mapper *NoteCodeMapper) DelNoteCode(code string, noteid uint64, systemId uint64) bool {
	db := orm.NewOrm()
	_, err := db.Raw("delete from xk_note_code where opid = ? and code = ? and mark = 0 and system_id = ?", noteid, code, systemId).Exec()
	if nil != err {
		return false
	}
	return true
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 标识兑换码已被使用
 */
func (mapper *NoteCodeMapper) UpdateNoteCode(code string, noteid uint64, systemId uint64) bool {
	db := orm.NewOrm()
	_, err := db.Raw("update xk_note_code set mark = 1 where opid = ? and code = ? and system_id = ?", noteid, code, systemId).Exec()
	if nil != err {
		return false
	}
	return true
}
